package com.prj.ufdm.cloud.ms.auth.bpo._impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.prj.ufdm.cloud.base.exception.BaseException;
import com.prj.ufdm.cloud.base.util.BaseUuidUtil;
import com.prj.ufdm.cloud.ms.auth.bpo.SysDutyBPO;
import com.prj.ufdm.cloud.ms.auth.dao.SysDutyDao;
import com.prj.ufdm.cloud.ms.auth.dao.SysUserDutyDao;
import com.prj.ufdm.cloud.ms.auth.enums.DutyTypeEnum;
import com.prj.ufdm.cloud.ms.auth.enums.SysUserStatusEnum;
import com.prj.ufdm.cloud.ms.auth.model.SysDuty;
import com.prj.ufdm.cloud.ms.core.bpo._impl.BaseBPOImpl;

/**
 * <p>
 * 职务管理 服务实现类
 * </p>
 *
 * @author zhengzixiang
 * @since 2019-09-19
 */
@Service(value="sysDutyBPO")
public class SysDutyBPOImpl extends BaseBPOImpl<SysDutyDao, SysDuty>  implements SysDutyBPO {

    @Autowired
    private  SysDutyDao sysDutyDao;
    @Autowired
    private  SysUserDutyDao sysUserDutyDao;
    
    @Override
	public List<SysDuty> doGetList(SysDuty paramObject) throws Exception
	{
    	List<SysDuty> list= sysDutyDao.selectList(paramObject);
    	for (SysDuty sysDuty : list) {
    		sysDuty.setDutyEnabledState(SysUserStatusEnum.getMessageByCode(sysDuty.getDutyEnabledState()));    		
    		String organNames="";   		  		
    		sysDuty.setOrganNames(organNames);
    	}
    	//根据职务di查询，职务机构表
		return list;
	}

    @Override
    @Transactional
	public int doSave(SysDuty paramObject) throws Exception{
    	//职务名称不能为空，
    	if(StringUtils.isBlank(paramObject.getDutyName())) {
    		throw new BaseException("职务名称不能为空！");
    	}
    	//职务不能同名
    	SysDuty sysDuty=new SysDuty();
    	sysDuty.setDutyName(paramObject.getDutyName());
    	List<SysDuty> list=sysDutyDao.selectList(sysDuty);
    	if(!CollectionUtils.isEmpty(list)) {
    		throw new BaseException("已存在相同的职务名称！");
    	}
    	String dutyId=BaseUuidUtil.getUUID();
    	paramObject.setId(dutyId);
    	paramObject.setDutyCode(BaseUuidUtil.getUUID());
    	paramObject.setDutyType(DutyTypeEnum.DUTY_TYPE_1.getCode());//职务类型设置为1
    	//默认状态为启用
    	if(StringUtils.isBlank(paramObject.getDutyEnabledState())) {
    		paramObject.setDutyEnabledState(SysUserStatusEnum.SYSUSER_STATUS_1.getCode());
    	} 		
		return sysDutyDao.insertOne(paramObject);
	}
    
    @Override
	public int doModById(SysDuty paramObject) throws Exception{
    	//id不能为空
    	if(StringUtils.isBlank(paramObject.getId())) {
    		throw new BaseException("更新id，不能为空");
    	}
    	//职务名称不能为空，
    	if(StringUtils.isBlank(paramObject.getDutyName())) {
    		throw new BaseException("职务名称不能为空！");
    	}
    	//不能修改为其它职务名相同
    	SysDuty sysDuty=new SysDuty();
    	sysDuty.setDutyName(paramObject.getDutyName());
    	List<SysDuty> list=sysDutyDao.selectList(sysDuty);
    	if(!CollectionUtils.isEmpty(list)) {
    		for (SysDuty duty : list) {
				if(!paramObject.getId().equals(duty.getId()) && paramObject.getDutyName().equals(duty.getDutyName())) {
					throw new BaseException("已存在相同的职务名称！");
				}
			}
    	}
		return sysDutyDao.updateById(paramObject);
	}

	
	@Override
	@Transactional
	public int doRmById(String dutyId) throws Exception
	{
		sysUserDutyDao.deleteUserDutyByDutyId(dutyId);;				
		return sysDutyDao.deleteById(dutyId);
	}

	/**
	 * DESC: 根据用户查询，查询用户职务
	 * @auther 胡义振
	 * @date 2020年1月17日
	 * @param userId
	 * @return 
	 * @return
	 */
	@Override
	public List<SysDuty> doGetListByUserId(String userId) {
		return sysDutyDao.selectListByUserId(userId);
	}

}
